<?php

class season_parser
{
    public static function get_season($movieid, $title, $s)
    {
        if($s=='x')
        {
            $season['series_title'] = '';
            $season['title'] = '';
            $season['search'] = '';
            $episode['number'] = 0;
            $episode['title'] = 'All Episodes';
            $season['episodes'] = array($episode);
        }
        else
        {
            $url = "http://www.imdb.com/title/$movieid/episodes/_ajax?season=$s";
            $content = udnutil::read_url($url);
            $content = str_replace('&nbsp;',' ', $content);
            $content = str_replace('&raquo;',' ', $content);
            $content = str_replace('itemscope',' ', $content);
            $content = str_replace('.jpg">','.jpg"/>', $content);
            $content = str_replace('.png">','.png"/>', $content);
            $content = str_replace('<hr>','', $content);

            $content = '<div>'.$content.'</div>';

            $doc = new DOMDocument();
            @$doc->loadHTML($content);
            $xpath = new DOMXPath($doc);


            //echo $title;
            $bracketIndex = strpos($title,'(');
            $season_title = substr($title,0,$bracketIndex-1);
            //echo $season_title;
            $season_title = str_replace(' ','.',$season_title);
            $season_search = $season_title.'.S'.(strlen($_GET['season']) > 1 ? $_GET['season'] : '0'.$_GET['season']);
            //echo $season_search;
            $xpathEpisode['number'] = '//div[@class="list detail eplist"]/div[{0}]//meta[@itemprop="episodeNumber"]';//@content
            $xpathEpisode['title'] = '//div[@class="list detail eplist"]/div[{0}]//a[@itemprop="name"]';
            $xpathEpisode['id'] = '//div[@class="list detail eplist"]/div[{0}]//a[@itemprop="url"]/div'; // @data-const
            $xpathEpisode['date'] = '//div[@class="list detail eplist"]/div[{0}]//div[@class="airdate"]';
            $xpathEpisode['description'] = '//div[@class="list detail eplist"]/div[{0}]//div[@itemprop="description"]';;
            $xpathEpisode['poster'] = '//div[@class="list detail eplist"]/div[{0}]//a[@itemprop="url"]/div/img';//@src

            $episodeList = $xpath->query('//div[@class="list detail eplist"]/div');

            $i = 1;
            $episodes = array();
            foreach($episodeList as $ep)
            {
                $episode['number'] = $xpath->query(str_replace('{0}',$i,$xpathEpisode['number']))->item(0)->getAttribute('content');
                $episode['title'] = htmlspecialchars($xpath->query(str_replace('{0}',$i,$xpathEpisode['title']))->item(0)->nodeValue);
                $episode['id'] = $xpath->query(str_replace('{0}',$i,$xpathEpisode['id']))->item(0)->getAttribute('data-const');
                $episode['date'] = $xpath->query(str_replace('{0}',$i,$xpathEpisode['date']))->item(0)->nodeValue;
                $episode['description'] = htmlspecialchars($xpath->query(str_replace('{0}',$i,$xpathEpisode['description']))->item(0)->nodeValue);
                $episode['poster'] = udnutil::dlurl(udnutil::smallImdbImage($xpath->query(str_replace('{0}',$i,$xpathEpisode['poster']))->item(0)->getAttribute('src')));
                $episode['season'] = strlen($_GET['season']) > 1 ? $_GET['season'] : '0'.$_GET['season'];
                $epnum = strlen($episode['number']) > 1 ? $episode['number'] : '0'.$episode['number'];
                $episode['search'] = $season_title.'.S'.$episode['season'].'E'.$epnum;
                $episodes[$i] = $episode;
                $i++;
            }

            $season['series_title'] = $season_title;
            $season['title'] = $title;
            $season['search'] = $season_search;
            $season['episodes'] = $episodes;
        }
        return season_parser::TestBlockHTML($season);
    }
    public static function TestBlockHTML($season)
    {
        ob_start(); ?>
        <?foreach($season['episodes'] as $ep){?>
        <option title="<?=$ep['title']?>" value="<?=$ep['number']?>" data-description="<?=$ep['title']?>" data-imagesrc="<?=$ep['poster']?>">
            Episode <?=$ep['number']?> (<?=$ep['date']?>)
        </option>
        <?}?>
        <?php return ob_get_clean();
    }
}
?>

